
class Solution {
    public static String reverseWords(String s) {
        String[] arr = s.split(" ");
        StringBuilder result = new StringBuilder();
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i].isEmpty()) continue;
            result.append(arr[i]);
            if (i == 0) break;
            result.append(" ");
        }
        String answer = result.toString();
        answer = answer.trim();
        return answer;
    }

    public static void main(String[] args) {
        reverseWords("  hello world!  ");
    }
}